<?php
/**
 * 招商线索
 */
namespace app\model;

use think\Model;

class BusinessclueModel extends Model
{
	protected $name = 'Businessclue';
	/**
	 * 招商线索列表
	 *
	 */
	public function GetList($where){
		$BusinessclueModel=new BusinessclueModel;
		$result=$BusinessclueModel->alias('a')->field('a.id,a.name,a.company_name,a.identification,a.from,a.address,a.updatetime,a.status,b.mobile,b.nickname')->join('admin b','a.user_id=b.id','LEFT')->where($where)->paginate()->toArray();
		foreach ($result['data'] as &$val){
			$val['updatetime']=date('Y-m-d H:i:s',$val['updatetime']);
		}
		return $result;
	}
	/**
	 * 招商线索详情
	 *
	 */
	public function detail($id){
		$BusinessclueModel = BusinessclueModel::find($id);
		return $BusinessclueModel;
	}
	/**
	 * 添加
	 *
	 */
	public function AddData($param){
		$BusinessclueModel = new BusinessclueModel;
		$param['createtime']=time();
		$param['updatetime']=time();
		$param['from']=2;
		if(isset($param['company_name'])&&!empty($param['company_name'])){
			$CompanyModel=new CompanyModel();
			$param['identification']=$CompanyModel->GetCompanyIdentificationByName($param['company_name']);
		} 
		$field=['name','company_name','identification','desc','address','from','createtime','updatetime','role_id','user_id'];
		$BusinessclueModel->allowField($field)->save($param);
		return $BusinessclueModel->id;
	}
	/**
	 * 编辑
	 *
	 */
	public function EditData($param,$id){
		$BusinessclueModel = BusinessclueModel::find($id);
		$param['updatetime']=time();
		
		$field=['name','company_name','identification','desc','address','updatetime','role_id'];
		if(isset($param['company_name'])&&!empty($param['company_name'])){
			$CompanyModel=new CompanyModel();
			$param['identification']=$CompanyModel->GetCompanyIdentificationByName($param['company_name']);
		} 
		$BusinessclueModel->allowField( $field)->save($param);
		return $id;
	}
	/**
	 * 修改禁用状态
	 *
	 */
	public function UpdateStatus($id){
		$BusinessclueModel = BusinessclueModel::find($id);
		$BusinessclueModel->status=$BusinessclueModel->status==1?0:1;
		$BusinessclueModel->save();
		return true;
	}
	/**
	 * 删除
	 *
	 */
	public function Del($param){
		$update=[];
		foreach($param as $val){
			$update[]=['id'=>$val,'status'=>-1,'deletetime'=>time()];
		}
		if(!empty($update)){
			$BusinessclueModel = new BusinessclueModel;
			$BusinessclueModel->saveAll($update);
		}
		return true;
	}
}